import info.gridworld.actor.Actor;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.awt.Color;

public class Jumper extends Actor{
	public Jumper() {
		setColor(Color.RED);
	}
	public Jumper(Color jumperColor) {
		setColor(jumperColor);
	}
	
	public void act() {
		if(canJump())
			jump();
		else
			turn();
	}
	
	public void turn() {
		setDirection(getDirection() + Location.HALF_RIGHT);
	}
	
	public void jump() {
		Grid<Actor> gr = getGrid();
		if(gr == null)
			return;
	
		Location loc = getLocation();
		Location next = loc.getAdjacentLocation(getDirection());
		Location next2 = next.getAdjacentLocation(getDirection());
		
		if(gr.isValid(next2))
			moveTo(next2);
		else
			removeSelfFromGrid();
	}
	
	public boolean canJump() {
		Grid<Actor> gr = getGrid();
		
		if(gr == null)
			return false;
		
		Location loc = getLocation();
		Location next = loc.getAdjacentLocation(getDirection());
		Location next2 = next.getAdjacentLocation(getDirection());
		
		if(!gr.isValid(next))
			return false;
		if(!gr.isValid(next2))
			return false;
		
		Actor neighbor = gr.get(next);
		Actor neighbor2 = gr.get(next2);
		if(!((neighbor == null) || (neighbor instanceof Flower) || (neighbor instanceof Rock) || (neighbor instanceof Actor)))
			return false;
		
		return (neighbor2 == null) || (neighbor2 instanceof Flower);
	}
}